import { addOrUpdateEmployeeAPI, deleteEmployeeAPI, fetchEmployeeDetailAPI, fetchEmployeeListAPI, updateStatusAPI } from '@/api/employeeAPI'

const actions = {
  /* 获取员工的action */
  async fetchEmployeeListAction(context, pageParams) {
    const { data } = await fetchEmployeeListAPI(pageParams)
    context.commit('setEmployeeList', data)
  },
  /* 删除员工的action */
  async deleteEmployeeAction(context, id) {
    await deleteEmployeeAPI(id)
    return '删除员工成功'
  },
  /* 更新员工状态的action */
  async updateStatusAction(context, { status, id }) {
    await updateStatusAPI(status, id)
    return status === 1 ? '启用成功' : '禁用成功'
  },
  /* 根据ID查询员工的action */
  async fetchEmployeeDetailAction(context, id) {
    const { data } = await fetchEmployeeDetailAPI(id)
    return data
  },
  /* 添加或者编辑员工的action */
  async addOrUpdateEmployee(context, employee) {
    await addOrUpdateEmployeeAPI(employee)
    return employee.id ? '编辑员工成功' : '添加员工成功'
  }
}

const mutations = {
  /* 设置员工列表、员工总数的mutation */
  setEmployeeList(state, { total, records }) {
    state.records = records
    state.total = total
  }
}

const state = {
  records: [],
  total: 0
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
